import wepy from 'wepy';

export default {
  jsonToUrl : (param, key) => {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
      paramStr += "&" + key + "=" + encodeURIComponent(param);
    } else {
      for (var p in param) {
        if (typeof(param[p]) == "function") {
          param[p]();
        } else {
          if (typeof(param[p]) == 'undefined') {
            paramStr += "&" + p + "=";
          } else {
            paramStr += "&" + p + "=" + param[p];
          }

        }
      }
    }
    return paramStr.substr(1);
  },
  /**
   * 重构房型数据
   * @param  {[type]} type [预留]
   * @param  {[type]} data [房型数据]
   * @return {[type]}      [重构后的房型数据]
*/
  reHomeTyprData(type, roomTypeList, vipCardTypeCode) {
    let redata = []
    if (typeof(roomTypeList) != "undefined") {
      for (let roomType of roomTypeList) { //房型循坏
        if (roomType.agreeUnitPrice != null) {
          //有会员协议价，使用会员协议规则
          roomType.caterName = '无早'
          if (roomType.hasSellBreakfast) {
            if (roomType.agreeCaterPrice != null && roomType.caterSellNum != undefined && roomType.caterSellNum != null) {
              let caters = roomType.caterSellNum.split(",")
              let temp = []
              for (let c of caters) {
                let n = 0
                let s = ''
                if (!isNaN(c)) {
                  n = (c - roomType.agreeRoomResvFreeNumber) > -1
                    ? (c - roomType.agreeRoomResvFreeNumber)
                    : 0
                  switch (c) {
                    case '1':
                      s = '单'
                      break;
                    case '2':
                      s = '双'
                      break;
                    case '3':
                      s = '三'
                      break;
                    case '4':
                      s = '四'
                      break;
                    case '5':
                      s = '五'
                      break;
                    case '6':
                      s = '六'
                      break;
                    case '7':
                      s = '七'
                      break;
                    case '8':
                      s = '八'
                      break;
                    case '9':
                      s = '九'
                      break;
                    case '10':
                      s = '十'
                      break;
                    default:
                      s = '无'
                      break
                  }
                  temp.push({
                    name: s + '早',
                    price: parseInt(roomType.agreeUnitPrice) + parseInt(roomType.agreeCaterPrice) * parseInt(n),
                    shopPrice: parseInt(roomType.shopPrice),
                    homePrice: roomType.curtVipPrice
                  });
                }
              }
              if (temp.length > 0) {
                roomType.curtVipPrice = temp[0].price //房型＋早餐价格
                roomType.caterName = temp[0].name //早餐单独费用
              } else {
                roomType.curtVipPrice = roomType.agreeUnitPrice //房型＋早餐价格
              }
              roomType.homePrice = roomType.agreeUnitPrice //房型会员原价
              roomType.caterPrice = temp.agreeCaterPrice //早餐单独费用
              roomType.caterLength = temp.length //早餐列表个数
              roomType.caterShopPrice = temp.shopPrice //门市价
              roomType.caterFreeNumber = temp.agreeRoomResvFreeNumber //免费份数
              roomType.caterList = temp
              roomType.caterActiveIndex = 0
              roomType.homeTypeIndex = -1
              console.log(roomType.caterList);

            } else {
              roomType.homePrice = roomType.agreeUnitPrice //早餐单独费用
              roomType.curtVipPrice = roomType.agreeUnitPrice //早餐单独费用
              roomType.caterName = '无早' //早餐单独费用
              roomType.caterPrice = 0 //早餐单独费用
              roomType.caterLength = 0
              roomType.caterLength = 0 //早餐单独费用
              roomType.caterShopPrice = 0 //门市价
              roomType.caterSellId = '' //ID
              roomType.caterFreeNumber = 0 //免费份数
              roomType.caterList = []
            }
          } else {
            roomType.homePrice = roomType.agreeUnitPrice //早餐单独费用
            roomType.curtVipPrice = roomType.agreeUnitPrice //早餐单独费用
            roomType.caterName = '无早' //早餐单独费用
            roomType.caterPrice = 0 //早餐单独费用
            roomType.caterLength = 0
            roomType.caterLength = 0 //早餐单独费用
            roomType.caterShopPrice = 0 //门市价
            roomType.caterSellId = '' //ID
            roomType.caterFreeNumber = 0 //免费份数
            roomType.caterList = []
          }

        } else {
          //无协议价，使用会员体系价格
          if (roomType.vipPrices != null) {
            roomType.caterName = '无早'
            for (let vipCard of roomType.vipPrices) { //会员卡价格循环
              if (!roomType['vipCard' + vipCard.vipCardTypeCode]) {
                if (vipCard.vipPrice == null || vipCard.vipPrice == '') {
                  vipCard.vipPrice = roomType.shopPrice
                }
                roomType['vipCard' + vipCard.vipCardTypeCode] = vipCard
              }
            }
          } else {
            roomType.caterName = '无早'
          }

          if (roomType.hasSellBreakfast) { //销售早餐
            if (roomType.lstCaterVipPrice == null) {
              roomType.homePrice = roomType.curtVipPrice //早餐单独费用
              roomType.curtVipPrice = roomType.curtVipPrice //早餐单独费用
              roomType.caterName = '无早' //早餐单独费用
              roomType.caterPrice = 0 //早餐单独费用
              roomType.caterLength = 0
              roomType.caterLength = 0 //早餐单独费用
              roomType.caterShopPrice = 0 //门市价
              roomType.caterSellId = '' //ID
              roomType.caterFreeNumber = 0 //免费份数
              roomType.caterList = []
              console.log('早餐数据配置错误');
            } else {
              for (let cater of roomType.lstCaterVipPrice) {
                if (roomType['vipCard' + cater.hotelVipCardCode]) {
                  roomType['vipCard' + cater.hotelVipCardCode].homePrice = roomType['vipCard' + cater.hotelVipCardCode].vipPrice //房型会员原价
                  roomType['vipCard' + cater.hotelVipCardCode].caterPrice = cater.sellVipPrice //早餐单独费用
                  roomType['vipCard' + cater.hotelVipCardCode].caterShopPrice = cater.shopPrice //门市价
                  roomType['vipCard' + cater.hotelVipCardCode].caterSellId = cater.caterSellId //ID
                  roomType['vipCard' + cater.hotelVipCardCode].caterFreeNumber = cater.roomResvFreeNumber //免费份数
                  roomType['vipCard' + cater.hotelVipCardCode].caterActiveIndex = 0 //餐饮激活索引号
                  roomType['vipCard' + cater.hotelVipCardCode].homeTypeIndex = -1 //房型激活索引号

                  let temp = []
                  if (cater.caterSellNum != '' && cater.caterSellNum != undefined && roomType['vipCard' + cater.hotelVipCardCode].vipPrice != undefined) {
                    let caters = cater.caterSellNum.split(",")
                    for (let c of caters) {
                      let n = 0
                      let s = ''
                      if (!isNaN(c)) {
                        n = (c - cater.roomResvFreeNumber) > -1
                          ? (c - cater.roomResvFreeNumber)
                          : 0
                        switch (c) {
                          case '1':
                            s = '单'
                            break;
                          case '2':
                            s = '双'
                            break;
                          case '3':
                            s = '三'
                            break;
                          case '4':
                            s = '四'
                            break;
                          case '5':
                            s = '五'
                            break;
                          case '6':
                            s = '六'
                            break;
                          case '7':
                            s = '七'
                            break;
                          case '8':
                            s = '八'
                            break;
                          case '9':
                            s = '九'
                            break;
                          case '10':
                            s = '十'
                            break;
                          default:
                            s = '无'
                            break
                        }
                        temp.push({
                          name: s + '早',
                          price: parseInt(roomType['vipCard' + cater.hotelVipCardCode].vipPrice) + parseInt(cater.sellVipPrice) * parseInt(n),
                          shopPrice: parseInt(roomType.shopPrice),
                          homePrice: roomType['vipCard' + cater.hotelVipCardCode].vipPrice
                        });
                      }
                    }
                  }
                  roomType['vipCard' + cater.hotelVipCardCode].caterList = temp

                  if (cater.hotelVipCardCode == vipCardTypeCode) {
                    if (temp.length > 0) {
                      roomType.curtVipPrice = temp[0].price //房型＋早餐价格
                      roomType.caterName = temp[0].name //早餐单独费用
                    } else {
                      roomType.curtVipPrice = roomType['vipCard' + cater.hotelVipCardCode].vipPrice //房型＋早餐价格
                    }
                    roomType.homePrice = roomType['vipCard' + cater.hotelVipCardCode].vipPrice //房型会员原价
                    roomType.caterPrice = cater.sellVipPrice //早餐单独费用
                    roomType.caterLength = temp.length //早餐列表个数
                    roomType.caterShopPrice = cater.shopPrice //门市价
                    roomType.caterSellId = cater.caterSellId //ID
                    roomType.caterFreeNumber = cater.roomResvFreeNumber //免费份数
                    roomType.caterList = temp
                    roomType.caterActiveIndex = 0
                    roomType.homeTypeIndex = -1
                  }
                }

              }
            }

          } else {
            roomType.homePrice = roomType.curtVipPrice //早餐单独费用
            roomType.curtVipPrice = roomType.curtVipPrice //早餐单独费用
            roomType.caterName = '无早' //早餐单独费用
            roomType.caterPrice = 0 //早餐单独费用
            roomType.caterLength = 0
            roomType.caterLength = 0 //早餐单独费用
            roomType.caterShopPrice = 0 //门市价
            roomType.caterSellId = '' //ID
            roomType.caterFreeNumber = 0 //免费份数
            roomType.caterList = []
          }

          //delete roomType.lstCaterVipPrice
        }

        redata.push(roomType)
      }
      return redata
    } else {
      return redata
    }
  },
  /**
   * [refHomeListData description]
   * @param  {[type]} type         [description]
   * @param  {[type]} homeList     [description]
   * @param  {[type]} orderList    [description]
   * @param  {[type]} roomTypeCode [description]
   * @return {[type]}              [description]
   */
  refHomeListData : (type, homeList, orderList, roomTypeCode) => {
    let ref_data = {
      room: {},
      allRoom: [],
      floor: []
    };
    for (var item of homeList) {

      let floor = parseInt(item.floorNo)
      if (ref_data.floor.indexOf(floor) == -1) {
        ref_data.floor.push(floor)
      }
      let f = 'f-' + floor;
      if (!ref_data.room[f]) {
        ref_data.room[f] = [];
      }
      item.status = '-1'
      ref_data.room[f].push(item)
      ref_data.allRoom.push(item)
    }
    if (orderList) {
      for (var i of orderList) {
        if ((i.roomTypeCode == roomTypeCode) && (i.status == '1' || i.status == '2' || i.status == '3')) {
          let floor = parseInt(i.floorNo)
          let f = 'f-' + floor;
          if (!ref_data[f]) {
            ref_data[f] = [];
          }
          i.remark = '点击查看订单详情'
          ref_data.room[f].unshift(i)
        }
      }
    }
    ref_data.floor = ref_data.floor.sort(function(a, b) {
      return a - b
    })
    return ref_data;

  },
  reHotelImg : (list) => {
    let temp = []
    for (let item of list) {
      if (item.hotelImgs != null) {
        for (let img of item.hotelImgs) {
          temp.push({name: item.itemName, imgUrl: img.imgUrl})
        }
      }
    }
    return temp
  },
  /**
   * 判断是否午夜（午夜房）
   * @return {Boolean} [description]
   */
  isMidnight(hotelConfig) {
    let nightRoomStartTime = hotelConfig.nightRoomStartTime
    let nightRoomEndTime = hotelConfig.nightRoomEndTime
    if (nightRoomStartTime == null || nightRoomEndTime == null || nightRoomEndTime == '' || nightRoomStartTime == '' || nightRoomStartTime == nightRoomEndTime) {
      return false
    }
    let d = new Date()
    let year = d.getFullYear()
    let month = d.getMonth() + 1
    let day = d.getDate()
    let n = d.getTime()
    let s = new Date(year + "/" + month + "/" + day + " " + nightRoomStartTime.substring(0, 2) + ":" + nightRoomStartTime.substring(2) + ":00").getTime()
    let e = new Date(year + "/" + month + "/" + day + " " + nightRoomEndTime.substring(0, 2) + ":" + nightRoomEndTime.substring(2) + ":59").getTime()
    if (n > s && n < e) {
      return true
    } else {
      return false
    }
  },

  /**
   * 判断订单保留多久
   * @return {Boolean} [description]
   */
  isSaveOrder(hotelConfig) {
    let nightRoomEndTime = hotelConfig.nightRoomEndTime
    if (nightRoomEndTime == null || nightRoomEndTime == '') {
      nightRoomEndTime = '0600'
    }
    let d = new Date()
    let n = d.getTime()
    let year = d.getFullYear()
    let month = d.getMonth() + 1
    let day = d.getDate()
    let houth = d.getHours()
    let sdate = year + "/" + month + "/" + day


    let s1 = new Date(sdate + " 18:00:00").getTime()
    let e1 = new Date(sdate + " 23:59:59").getTime()
    let s2 = new Date(sdate + " 00:00:00").getTime()
    let e2 = new Date(sdate + " " + nightRoomEndTime.substring(0, 2) + ":" + nightRoomEndTime.substring(2) + ":59").getTime()
    console.log('s1=' + s1);
    console.log('e1=' + e1);
    console.log('s2=' + s2);
console.log('e2=' + e2);
    if ((n > s1 && n < e1) || (n > s2 && n < e2)) {
      return false
    } else {
      return true
    }
  },
  /**
   * 获得时间描述
   * @param  {[type]} time [description]
   * @return {[type]}      [description]
   */
  getTimeDes(time) {
    let h = time.substring(0, 2)
    let ph = parseInt(h)
    let t = h + ':' + time.substring(2)
    if (ph <= 5 && ph > 3) {
      return '凌晨' + t
    } else if (ph < 7) {
      return '清晨' + t
    } else if (ph < 12) {
      return '上午' + t
    } else if (ph = 12) {
      return '中午' + t
    } else if (ph > 12) {
      return '下午' + t
    } else if (ph > 18) {
      return '傍晚' + t
    } else if (ph > 20 || ph <= 3) {
      return '晚上' + t
    } else {
      return t
    }
  },
  /**
   * 计算几天后的日期
   * @param  {[type]} date   [开始日期，格式 ‘2017-08-08’]
   * @param  {[type]} day    [加减天数]
   * @param  {[type]} reType [返回类型 reType==1和其他未知类型返回YYYY-MM-DD格式  reType==2返回MM-DD格式  reType==3返回YYYY年MM月DD日格式 reType==4返回MM月DD日格式]
   * @return {[type]}        [description]
   */
  countDate(date, day, reType) {
    date = date.replace(/\-/g, '\/');
    let dd = new Date(date)
    let d = dd.setDate(dd.getDate() + day)
    let dc = new Date(d);
    let dcy = dc.getFullYear()
    let dcm = (dc.getMonth() + 1) > 9
      ? (dc.getMonth() + 1)
      : '0' + (dc.getMonth() + 1)
    let dcd = (dc.getDate()) > 9
      ? (dc.getDate())
      : '0' + (dc.getDate())
    if (reType == 2) {
      return dcm + '-' + dcd
    } else if (reType == 3) {
      return dcy + '年' + dcm + '月' + dcd + '日'
    } else if (reType == 4) {
      return dcm + '月' + dcd + '日'
    } else {
      return dcy + '-' + dcm + '-' + dcd
    }
  },
  /**
   * 计算预定天数
   * @param  {[type]} start [开始日期 格式 YYYY-MM-DD]
   * @param  {[type]} end   [结束日期 格式 YYYY-MM-DD]
   * @return {[type]}       [description]
   */
  sumDate(start, end) {
    console.log(start);
    console.log(end);
    start = start.replace(/\-/g, '\/');
    start = new Date(start).getTime();
    end = end.replace(/\-/g, '\/');
    end = new Date(end).getTime();
    return Math.abs((end - start)) / (1000 * 60 * 60 * 24)
  },
  /**
   * 判断是否今日入住
   * @param  {[type]} start [需要判断的日期 格式 YYYY-MM-DD]
   * @return {[type]}       [description]
   */
  todayDate(start) {
    start = start.replace(/\-/g, '\/');
    start = new Date(start).getTime();
    let d = new Date()
    let m = d.getMonth() + 1
    let end = new Date(d.getFullYear() + '/' + m + '/' + d.getDate()).getTime()
    if (end >= start) {
      return true
    } else {
      return false
    }
  },
  /**
   * 对象转url
   * @param  {[type]} start [需要判断的日期 格式 YYYY-MM-DD]
   * @return {[type]}       [description]
   */
  http_builder_url(url, data) {
    if(typeof(url) == 'undefined' || url == null || url == '') {
                return '';
            }
            if(typeof(data) == 'undefined' || data == null || typeof(data) != 'object') {
                return '';
            }
            url += (url.indexOf("?") != -1) ? "" : "?";
            for(var k in data) {
                url += ((url.indexOf("=") != -1) ? "&" : "") + k + "=" + encodeURI(data[k]);
                console.log(url);
            }
            return url;
  }
};
